package com.misolab.game.sbh.data.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.misolab.game.sbh.data.db.table.ITable;
import com.misolab.game.sbh.util.log.Logger;

public class DBHelper extends SQLiteOpenHelper implements DBConstant {

	final static String ClassName = DBHelper.class.getSimpleName();
	static void logFunc(String name) {
		Logger.i(ClassName + "::" + name + "()");
	}

	public static final String BEGIN = "BEGIN;";
	public static final String ROLLBACK = "ROLLBACK;";
	public static final String COMMIT = "COMMIT;";

	ArrayList<ITable> mTableList = new ArrayList<ITable>();
	Context mContext = null;
	SQLiteDatabase mDB;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		logFunc("DBHelper");

		this.mContext = context;
	}

	public ArrayList<ITable> getmTableList() {
		logFunc("getmTableList");

		return mTableList;
	}




	@Override
	public void onCreate(SQLiteDatabase db) {
		logFunc("onCreate");

		for (ITable table : mTableList) {
			table.create(db);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		logFunc("onUpgrade");

		for (ITable table : mTableList) {
			table.upgrade(db, oldVersion, newVersion);
		}
	}
	
	public void begin() throws SQLException {
		logFunc("begin");

		//		db.beginTransaction();
		mDB.execSQL(BEGIN);
	}
	
	public void commit() throws SQLException  {
		logFunc("commit");

		mDB.execSQL(COMMIT);
//		db.endTransaction();
	}

	public void rollback() throws SQLException  {
		logFunc("rollback");

		mDB.execSQL(ROLLBACK);
//		db.endTransaction();
	}
	
	public void close() {
		logFunc("close");

		mDB.close();
	}
	
	public SQLiteDatabase open() {
		logFunc("open");

		mDB = getWritableDatabase();
		return mDB;
	}


	public SQLiteDatabase open(boolean readable) {
		logFunc("open");

		if (readable) {
			mDB = getReadableDatabase();
		} else {
			mDB = getWritableDatabase();
		}
		return mDB;
	}

	public Cursor rawQuery(String sql){
		logFunc("rawQuery");
		Logger.d("sql:"+sql);
		return mDB.rawQuery(sql, null);
	}
	
	public Cursor rawQuery(String sql, String[] selectionArgs){
		logFunc("rawQuery");
		Logger.d("sql:"+sql);
		for (int i = 0; i < selectionArgs.length; i++) {
			Logger.d("selectionArgs:"+selectionArgs[i]);			
		}

		return mDB.rawQuery(sql, selectionArgs);
	}
	
	public void rawExecSQL(String sql) throws SQLException {
		logFunc("rawExecSQL");
		Logger.d("sql:"+sql);

		mDB.execSQL(sql);
	}
	
	public void rawExecSQL(String sql, String[] values) throws SQLException {
		logFunc("rawExecSQL");
		Logger.d("sql:"+sql);
		for (int i = 0; i < values.length; i++) {
			Logger.d("selectionArgs:"+values[i]);			
		}

		mDB.execSQL(sql, values);
	}
}
